package com.lnking.lnkingplace.exception;


import com.lnking.common.http.ReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.NoPermissionException;
import javax.servlet.http.HttpServletRequest;
import java.net.BindException;
import java.text.SimpleDateFormat;

/**
 * 全局异常捕获器
 * @author 联坤
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    private String errFile = "/log/error.log";

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @ExceptionHandler({Throwable.class})
    public ReturnResult handleThrowable(Throwable e, HttpServletRequest request) {
        log.error("URL:{} , 系统异常：{}", request.getRequestURI(), e);
        return ReturnResult.error(ReturnResult.ResultStatus.OTHER_ERROR);
    }

    @ResponseBody
    @ExceptionHandler({BindException.class})
    public ReturnResult handleBindException(BindException e, HttpServletRequest request) {
        String failMsg = e.getMessage();
        log.error("URL:{} ,绑定异常:{} ", request.getRequestURI(), failMsg);
        return ReturnResult.error(ReturnResult.ResultStatus.INVALID_TOKEN);
    }

    @ResponseBody
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ReturnResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e, HttpServletRequest request) {
        //  从异常对象中拿到ObjectError对象
        ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
        String failMsg = objectError.getDefaultMessage();
        log.error("URL:{} ,参数校验异常:{} ", request.getRequestURI(), failMsg);
        //  然后提取错误提示信息进行返回
        return ReturnResult.error(failMsg);

    }


    /**
     * 处理空指针的异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler({NullPointerException.class})
    @ResponseBody
    public ReturnResult exceptionHandler(HttpServletRequest req, NullPointerException e) {
        log.error("发生空指针异常！原因是:", e);
        return ReturnResult.error(ReturnResult.ResultStatus.ERROR);
    }


    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ReturnResult baseErrorHandler(Exception ex) throws Exception {
        log.info(ex.toString());
        ex.printStackTrace();
        if (ex instanceof MethodArgumentNotValidException) {
            MethodArgumentNotValidException exception = (MethodArgumentNotValidException) ex;
            return ReturnResult.error(exception.getBindingResult().getFieldError().getDefaultMessage());
        } else if (ex instanceof NoPermissionException) {
            NoPermissionException exception = (NoPermissionException) ex;
            return ReturnResult.error(exception.getMessage());
        } else if (ex instanceof NumberFormatException) {
            NumberFormatException exception = (NumberFormatException) ex;
            return ReturnResult.error(ReturnResult.ResultStatus.ERROR);
        } else {
            return ReturnResult.error(ex.getMessage());
        }
    }
}
